package com.apac.services;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jmesa.core.filter.MatcherKey;
import org.jmesa.core.filter.NumberFilterMatcher;
import org.jmesa.limit.ExportType;
import org.jmesa.model.TableModel;

import com.apac.jmesa.RowEventJmesa;
import com.apac.jmesa.JmesaObjectList;
import com.apac.jmesa.VersatileDateFilterMatcher;

public class TableModelGeneratorService extends AbstractService{
	
	public static String generateTable(String tblName, HttpServletRequest request, HttpServletResponse response, JmesaObjectList items, RowEventJmesa reJmesa){
		
		TableModel tableModel = new TableModel(tblName, request, response);

		tableModel.setExportTypes(ExportType.CSV, ExportType.JEXCEL,ExportType.PDFP);
		tableModel.setItems(items);

		if (tableModel.isExporting()) {
			
			tableModel.setTable(TableGeneratorService.generateExportTable(items));
			String export = tableModel.render();
			return export;
			
		} else {
			tableModel.addFilterMatcher(new MatcherKey(Date.class), new VersatileDateFilterMatcher("MMMM dd, yyyy"));
			tableModel.addFilterMatcher(new MatcherKey(Double.class), new NumberFilterMatcher("###.##"));
			tableModel.addFilterMatcher(new MatcherKey(Integer.class), new NumberFilterMatcher("###"));
			tableModel.setTable(TableGeneratorService.generateHtmlTable(items, reJmesa));
			return tableModel.render();
			
		}
		
	}
		
}
